<template>
    <div class="my-image">
      <!-- 图片按钮 -->
      <div class="btn-image" @click="openDialog">
        <img :src="value || previewImg" alt="">
      </div>
      <!-- 对话框 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="720px">
        <!-- tabs组件 -->
      <el-tabs v-model="activeName" type="card" >
        <el-tab-pane label="素材库" name="images">
          <!-- 按钮 -->
          <el-radio-group @change="toggleList()" v-model="reqParams.collect" size="small">
            <el-radio-button :label="false">全部</el-radio-button>
            <el-radio-button :label="true">收藏</el-radio-button>

          </el-radio-group>
          <!--列表 -->
          <div class="ima-list">
            <div :class="{selected:selectedImgUrl===item.url}" @click="selectedImg(item.url)" class="img-item" v-for="item in images" :key="item.id">
              <img :src="item.url" alt="">
            </div>
          </div>
          <!-- 分页 -->
          <el-pagination background
            layout="prev, pager, next"
            :total="total"
            :page-size="reqParams.per_page"
            :current-page="reqParams.page"
            @current-change="changePager"
          ></el-pagination>
        </el-tab-pane>
        <el-tab-pane label="上传图片" name="upload">
          <el-upload
            class="avatar-uploader"
            action="http://ttapi.research.itcast.cn/mp/v1_0/user/images"
            name="image"
            :headers="headers"
            :show-file-list="false"
            :on-success="uploadSuccess">
            <img v-if="uplosdImgUrl" :src="uplosdImgUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-tab-pane>
      </el-tabs>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmImg()">确 定</el-button>
        </span>
      </el-dialog>

    </div>
</template>

<script>
import auth from '@/utils/auth'
// 导入图片
// import img from '../assets/default.png'
import Img from '../assets/default.png'
export default {
  name: "my-image",
  props:['value'],
  data(){
    return{
      // 控制对话框显示隐藏
      dialogVisible:false,
      // 当前激活选项卡的name是
      activeName:'images',
      // 查询素材参数对象
      reqParams:{
        collect:false,
        page:1,
        per_page:8
      },
      // 素材列表
      images:[],
      // 总条数
      total:0,
      // 当前选中的图片地址
      selectedImgUrl:null,
      // 上传的图片地址
      uplosdImgUrl :null,
     // 上传请求头对象
      headers: { Authorization: `Bearer ${auth.getUser().token}` },
      // 封面预览图
      previewImg :Img
    }
  },
  methods: {
    // 确认图片
    confirmImg(){
      if (this.activeName === 'images') {
        // 激活素材库
          if (!this.selectedImgUrl) return this.$message.warning('请选择一张图')
          // this.previewImg = this.selectedImgUrl
          // 把选中图片传给父组件使用
          this.$emit('input', this.selectedImgUrl)
      } else {
        // 激活 上传图片
        if (!this.uplosdImgUrl)  return this.$message.warning('请上传一张图片')
        // this.previewImg = this.uplosdImgUrl
        // 吧上传图片传给父组件使用
        this.$emit('input',this.uploadImgUrl)
      }
      // 关闭对话框
      this.dialogVisible = false
      // 在这个位置需要做其他组件的业务================
      // 在确认突破后，会触发my-image组件的自定义事件confirrm
      this.$emit('confirm')
    },
    // 上传图片成功
    uploadSuccess(response){
        // 预览
        this.uplosdImgUrl = response.data.url
    },
      // 记录点击的图片
    selectedImg (url) {
      this.selectedImgUrl = url
    },
    // 切换列表
    toggleList(){
      this.reqParams.page=1
      this.getImages()
    },
    // 分页
    changePager(np){
      this.reqParams.page = np
      this.getImages()
    },
    // 打开对话框
    openDialog(){
      this.dialogVisible = true
      //获取素材列表数据
      this.getImages()
      // 清除之前的操作记录
      this.activeName = 'images'
      this.selectedImgUrl = null
      this.uploadImgUrl = null
    },
    // 获取图片素材
    async getImages(){
      const res = await this.$http.get('user/images',{params:this.reqParams})
      this.images = res.data.data.results
      this.total = res.data.data.total_count
    }
  }
}
</script>

<style scoped lang="less">
  .my-image{
    display: inline-block;
    margin-right: 20px;
    .btn-image{
      width: 150px;
      height: 150px;
      border: 1px dashed #ddd;
      img{
        width: 100%;
        height: 100%;
        display: block;
      }
    }
    .ima-list{
      margin-top: 15px;
      .img-item{
        width: 150px;
        height: 120px;
        border: 1px dashed #ddd;
        display:inline-block;
        margin-right:15px ;
        position: relative;
        img{
          width: 100%;
          height: 100%;
          display: block;
        }
        &.selected::after {
        content: "";
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background: rgba(0,0,0,.3) url(../assets/selected.png) no-repeat center / 50px auto;
      }
      }
    }
  }
  ::v-deep .el-dialog__body{
    padding-bottom: 0;
  }
</style>
